package com.idopartx.phonelightning.widget.screen

import android.animation.ValueAnimator
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import android.view.animation.LinearInterpolator

class RoundedDashView : View {
    enum class Orientation {
        VERTICAL, HORIZONTAL
    }

    var dashRadius: Float = 10f
        set(value) {
            field = 10 + value

            path = Path()
            path.addCircle(0f, 0f, dashRadius, Path.Direction.CW)
            paint.pathEffect =
                PathDashPathEffect(path, dashRadius * 4, 0f, PathDashPathEffect.Style.ROTATE)
        }


    private val paint = Paint(Paint.ANTI_ALIAS_FLAG)
    private var path = Path()
    private var orientation = Orientation.HORIZONTAL
    private var screenWidth: Int = 0
    private var screenHeight: Int = 0
    private var animator: ValueAnimator? = null
    private lateinit var linearGradient: LinearGradient
    private var mGradientMatrix: Matrix = Matrix()

    var translateSpeed = 100
    set(value) {
        field = 100 - value * 5
    }

    private var translate = 0f

    constructor(context: Context?) : super(context) {
        init()
    }

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
        init()
    }

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        init()
    }

    constructor(
        context: Context?,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int
    ) : super(context, attrs, defStyleAttr, defStyleRes) {
        init()
    }

    private fun init() {
//        paint.strokeCap = Paint.Cap.ROUND
        paint.style = Paint.Style.STROKE
        path = Path()
        path.addCircle(0f, 0f, dashRadius, Path.Direction.CW)
        paint.pathEffect =
            PathDashPathEffect(path, dashRadius * 4, 0f, PathDashPathEffect.Style.ROTATE)
        animator = ValueAnimator.ofInt(0, 1, 2)
        animator?.duration = 5000
        animator?.interpolator = LinearInterpolator()
        animator?.repeatCount = ValueAnimator.INFINITE
        animator?.addUpdateListener {

            invalidate()
        }
        animator?.start()
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        screenWidth = w
        screenHeight = h

        val colors = intArrayOf(
            Color.parseColor("#4FBCB5"),
            Color.parseColor("#9D27DC"),
            Color.parseColor("#4FBCB5"),
            Color.parseColor("#9D27DC"),
            Color.parseColor("#4FBCB5")
        )
        val pos = floatArrayOf(0f, 0.5f, 1f)
        linearGradient =
            LinearGradient(
                0f,
                0f,
                400f,
                400f,
                colors,
                null,
                Shader.TileMode.REPEAT
            )
//        val matrix = Matrix()
//        linearGradient.setLocalMatrix(matrix)
//        paint.shader = linearGradient
    }

    override fun onDraw(canvas: Canvas) {
        paint.shader = linearGradient

        super.onDraw(canvas)
        translate += 400f / translateSpeed
        if (translate > 2 * measuredWidth + 2 * measuredHeight) {
            translate = 0f
        }

        mGradientMatrix.setTranslate(translate, translate)

        linearGradient.setLocalMatrix(mGradientMatrix)

        path.reset()
        val radius = 50f
        path.moveTo(50f, dashRadius)
        path.lineTo(screenWidth - radius, dashRadius)
        path.cubicTo(
            screenWidth - radius / 2,
            0f,
            screenWidth.toFloat(),
            radius / 2,
            screenWidth.toFloat() - dashRadius,
            radius
        )
        path.lineTo(screenWidth.toFloat() - dashRadius, screenHeight - radius)
        //
        path.cubicTo(
            screenWidth.toFloat(),
            screenHeight - radius / 2,
            screenWidth - radius / 2,
            screenHeight.toFloat(),
            screenWidth - radius - dashRadius,
            screenHeight.toFloat() - dashRadius
        )
        path.lineTo(radius, screenHeight.toFloat() - dashRadius)
        path.cubicTo(
            radius / 2,
            screenHeight.toFloat(),
            0f,
            screenHeight - radius / 2,
            dashRadius,
            screenHeight - radius
        )
        path.lineTo(dashRadius, radius)

        path.cubicTo(0f, radius / 2, radius / 2, 0f, radius, dashRadius)

        canvas.drawPath(path, paint)
        invalidate()
    }

    fun setOrientation(orientation: Orientation) {
        this.orientation = orientation
        invalidate()
    }
}